{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "35609088-8283-426f-a313-80fd4b5c18c2",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 回测结果查看"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50d26b82-6f44-4d95-bd23-fa01606d31c3",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from chanlun.backtesting import backtest\n",
    "from chanlun.config import get_data_path\n",
    "\n",
    "from chanlun.strategy.strategy_a_d_mmd_test import StrategyADMMDTest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "921761b5-7729-4b53-a705-898674fb88a7",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 从保存的回测落地文件中加载数据\n",
    "save_file = str(get_data_path() / \"backtest\" / \"a_w_mmd_v0_signal.pkl\")\n",
    "BT = backtest.BackTest()\n",
    "BT.load(save_file)\n",
    "# 显示回测配置与结果\n",
    "BT.info()\n",
    "BT.result()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ee7628a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存一个不包含策略对象与持仓附加信息的策略文件\n",
    "# BT.save_file = str(get_data_path() / 'backtest' / 'a_w_mmd_v0_signal_no_strategy.pkl')\n",
    "# BT.strategy = None\n",
    "# for _code, _poss in BT.trader.positions_history.items():\n",
    "#     for _p in _poss:\n",
    "#         _p.info = {}\n",
    "# BT.trader.balance_history = {}\n",
    "# BT.trader.hold_profit_history = {}\n",
    "# BT.trader.positions_balance_history = {}\n",
    "# BT.orders = {}\n",
    "# BT.save()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71f57d6a-3075-4990-ba2f-940b08cf865c",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 回测数据展示\n",
    "BT.backtest_charts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3030af77-4801-4bac-adb4-f5f1e14c3787",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 显示历史持仓\n",
    "#设置显示全部行，不省略\n",
    "pd.set_option('display.max_rows',None)\n",
    "#设置显示全部列，不省略\n",
    "pd.set_option('display.max_columns',None)\n",
    "\n",
    "# 读取策略中包含的附加信息与平仓uid信息\n",
    "info_keys = []\n",
    "close_uids = []\n",
    "for _, _poss in BT.trader.positions_history.items():\n",
    "    for _p in _poss:\n",
    "        info_keys += list(_p.info.keys())\n",
    "        close_uids += list(_p.close_uid_profit.keys())\n",
    "info_keys = list(sorted(list(set(info_keys))))\n",
    "close_uids = list(sorted(list(set(close_uids))))\n",
    "print(info_keys)\n",
    "print(close_uids)\n",
    "\n",
    "# 显示指定标的的持仓\n",
    "pos_df = BT.positions(add_columns=info_keys, close_uids=['clear'])\n",
    "pos_df['_win'] = pos_df['profit_rate'].apply(lambda p: int(p>0))\n",
    "pos_df.groupby(['mmd']).agg({'profit_rate': {'mean', 'sum', 'count'}, '_win': {'mean', 'sum', 'count'}})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5ed4573",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 过滤条件\n",
    "pos_querys = [\n",
    "    \n",
    "]\n",
    "for _q in pos_querys:\n",
    "    pos_df = pos_df.query(_q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ddf51f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun = lambda x: int(x >= 1)\n",
    "x_key = 'k_now_d_change'\n",
    "groupbys = ['opt_mmd']\n",
    "pos_df['_x'] = pos_df[x_key].apply(fun)\n",
    "pos_df.groupby(groupbys).agg({'profit_rate': {'mean', 'sum', 'count'}, '_win': {'count', 'sum', 'mean'}})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abf97517-55c7-4147-bf79-9fa8d21ac24e",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 显示标的周期的图标\n",
    "BT.show_charts(BT.codes[0], BT.frequencys[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36894871",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chanlun",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
